package calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JApplet implements ActionListener {
    JFrame jFrame = new JFrame();
    Container cp = getContentPane();
    JButton JButton1 = new JButton("确定");//创建JButton1，并初始化
    JLabel JLabel1 = new JLabel("+");
    JLabel JLabel2 = new JLabel("=");
    JTextField JTextField1 = new JTextField(10);
    JTextField JTextField2 = new JTextField(10);
    JTextField JTextField3 = new JTextField(10);

    public void init() {
        cp.setLayout(new FlowLayout());
        cp.add(JTextField1);
        cp.add(JLabel1);
        cp.add(JTextField2);
        cp.add(JLabel2);
        cp.add(JTextField3);
        cp.add(JButton1);
        JButton1.addActionListener(this);
        cp.setVisible(true);
        jFrame.add(cp);
        jFrame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==JButton1)//用getSource()方法获取事件源
        {
            e.getSource();
            double sum = Double.valueOf(JTextField1.getText()) + Double.valueOf(JTextField2.getText());//获取两个操作数，并转化为double型
            JTextField3.setText(String.valueOf(sum));
        }
    }


}